﻿using Flurl.Http;
using HandyControl.Controls;
using HandyControl.Tools.Extension;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Shell;
using wms_wpf.Dto;
using wms_wpf.InPut;

namespace wms_wpf.Apiservice
{
    public class BaseService : IBaseService
    {
      
        string  config = Environment.GetEnvironmentVariable("wms-config", EnvironmentVariableTarget.Machine);

        public async Task<string> Create_Boxs(InPut_BaseService input)
        {

            try
            {
                var url = $"http://{config}/api/BaseService/Create_Boxs";
                var data = input;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    Growl.Success(Response.Msg, "msg");
                    return Response.Msg;
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return Response.Msg;
                }

            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("Create_Boxs", ex);
                return null;
            }
        }//配置箱盒
        public async Task<string> Create_Warehouses(InPut_BaseService3 input)
        {
      
            try
            {
                var url = $"http://{config}/api/BaseService/Create_Warehouses";
                var data = input;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);   
                if (Response.State)
                {
                    Growl.Success(Response.Msg, "msg");
                    return Response.Msg;
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return Response.Msg;
                }
       
            }
            catch(Exception ex) 
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("Create_Warehouses", ex);
                return null;
            }
        }//配置库位
        public async Task<Tuple<List<BoxDto>, long?, long?>> GetList_Boxs(InPut_BaseService6 inPut)
        {
            try
            {
                var url = $"http://{config}/api/BaseService/GetList_Boxs";
                var data = inPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponsePage Response = JsonConvert.DeserializeObject<ApiResponsePage>(result);
                if (Response.State)
                {
                    List<BoxDto> res = JsonConvert.DeserializeObject<List<BoxDto>>(Response.Dto.ToString());
                    return Tuple.Create(res, Response.Count, Response.PageCount);
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return null;
                }


            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog(" GetList_Boxs", ex);
                return null;
            }
        }//获取箱盒表

        public async Task< Tuple< List<WarehouseLineDto>,long?,long?>> GetList_WarehouseLine(InPut_BaseService4 input)//获取库位表
        {
            try
            {
                var url = $"http://{config}/api/BaseService/GetList_WarehouseLine";
                var data = input;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponsePage Response = JsonConvert.DeserializeObject<ApiResponsePage>(result);         
                if (Response.State)
                {
                    List<WarehouseLineDto> res = JsonConvert.DeserializeObject<List<WarehouseLineDto>>(Response.Dto.ToString());              
                    return Tuple.Create(res, Response.Count, Response.PageCount);
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return null;
                }
              

            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("GetList_WarehouseLine", ex);
                return null;
            }
        }
        public async Task<bool> Update_WarehouseLines(InPut_BaseService5 inPut)
        {
            try
            {
                var url = $"http://{config}/api/BaseService/Update_WarehouseLines";
                var data = inPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponsePage Response = JsonConvert.DeserializeObject<ApiResponsePage>(result);
                if (Response.State)
                {
                    Growl.Success(Response.Msg, "msg");
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                }
                return true;

            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("Update_WarehouseLines", ex);
                return false;
            }
        }//更新库位状态

        public async Task<string> Update_Box(InPut_BaseService2 input)
        {
            try
            {
                var url = $"http://{config}/api/BaseService/Update_Box";
                var data = input;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    Growl.Success(Response.Msg, "msg");
                    return Response.Msg;
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return Response.Msg;
                }

            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("Update_Box", ex);
                return null;
            }
        }
    }
}
